package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.Question;
import com.example.service.QuestionService;
import com.example.mapper.QuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author DPH
 * @description 针对表【tbl_question】的数据库操作Service实现
 * @createDate 2022-11-23 16:21:08
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question>
        implements QuestionService{
    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public List<Question> getQuestions(int questionnaireId) {
        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        wrapper.eq("questionnaire_id",questionnaireId);
        return questionMapper.selectList(wrapper);
    }
    @Override
    public List<Question> getAllQuestions() {
        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        return questionMapper.selectList(wrapper);
    }


    @Override
    public Question modify(Question question) {
        return questionMapper.updateById(question) > 0 ? question : null;
    }



    @Override
    public Question findById(Integer questionnaireId, Integer qId) {
        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        wrapper.eq("questionnaire_id",questionnaireId);
        wrapper.eq("question_num",qId);
        return questionMapper.selectOne(wrapper);
    }

    @Override
    public boolean delete(Integer questionnaireId, Integer qId) {
        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        wrapper.eq("questionnaire_id",questionnaireId);
        wrapper.eq("question_num",qId);
        return questionMapper.delete(wrapper) > 0;
    }


    @Override
    public boolean delete(Integer questionnaireId) {
        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        wrapper.eq("questionnaire_id",questionnaireId);
        return questionMapper.delete(wrapper) > 0;
    }

    @Override
    public boolean add(Question question) {
        return questionMapper.insert(question) > 0;
    }

    @Override
    public List<Question> getMCQ(int questionnaireId) {
        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        wrapper.eq("questionnaire_id",questionnaireId);
        wrapper.eq("type","SChoice").or().eq("type","MChoice");
        return questionMapper.selectList(wrapper);
    }

    @Override
    public List<Question> getCloze(int questionnaireId) {
        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        wrapper.eq("questionnaire_id",questionnaireId);
         wrapper.eq("type","Blank");
        System.out.println("第一个："+questionMapper.selectList(wrapper));
        return questionMapper.selectList(wrapper);
    }
    @Override
    public int getQuestionNum() {
        return Math.toIntExact(questionMapper.selectCount(null));
    }
}